from aip import AipOcr
import configparser



class BaiDuPAI(object):
    '''用于识别文字'''

    def __init__(self):
        target = configparser.ConfigParser
        target.read('password.ini')
        APPID = target.get('我的工单', 'APPID')
        APIKEY = target.get('我的工单', 'APIKEY')
        SecreKey = target.get('我的工单', 'SecreKey')



        # 内类都可以使用
        self.client = AipOcr(APPID, APIKEY, SecreKey)

    # 读取图片
    @classmethod
    def getPicture(cls, filePath):
        with open(filePath, 'rb') as fp:
            return fp.read()

    def picture2Text(self, filePath):
        # 读取图片
        image = self.getPicture(filePath=filePath)
        # 识别图片
        text = self.client.basicGeneral(image)

        alltexts = ''
        for item in text('words_result'):
            alltexts = alltexts + ''.join(item.get('words', ''))

        return alltexts


if '__main__' == __name__:
    baiduapi = BaiDuPAI()
    text = baiduapi.picture2Text('imageGrab.png')
    print(text)